package cn.szzsi._admin;

import cn.szzsi.common.kit.CommonUtil;
import cn.szzsi.common.model.*;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 商户的用户管理
 * Created by yuanzheng on 2016/12/1.
 */
public class UserController extends BaseAdminController<Member> {
    private String shopId;
    public void list(){
        /*int pageNumber=getParaToInt("pageNumber", 1);
        int pageSize=getParaToInt("pageSize", 10);
        StringBuilder sql=new StringBuilder("from mall_member where b_shoprank='"+getUid()+"'");
        sql.append(" order by createDate desc ");
        Page<Member> pager = Member.dao.paginate(pageNumber,pageSize,"select * ",sql.toString());
        setAttr("pager", pager);*/
        render(CommonUtil.TEMPLATE_PATH+"/business/usermanage/user_manage_list.html");
    }

    //ajax list
    public void ajaxlist(){
        shopId=getAttr("shopId");
        List<Member> lm = Member.dao.find("select * from mall_member where b_shoprank = ? order by createDate desc",shopId);
        for(Member m:lm){
            m.init();
        }
        renderJson(lm);
    }

    // 添加
    public void add() {
        shopId=getAttr("shopId");
        setAttr("roleList", Role.dao.getByUid(shopId));
        setAttr("brandStoreList", BrandStore.dao.getByShopId(shopId));
        setAttr("shopId",shopId);
        render(CommonUtil.TEMPLATE_PATH+"/business/usermanage/user_manage_input.html");
    }
    // 编辑
    public void edit() {
        shopId=getAttr("shopId");
        String id = getPara("id","");
        Member member=Member.dao.findById(id);
        if(StrKit.notBlank(id)){
            setAttr("member", member);
        }
        String categories=member.getStr("categories");
        if(StrKit.notBlank(categories)){
            String arr[]=member.getStr("categories").split(",");
            setAttr("category_one",arr[0]);
            setAttr("category_two",arr[1]);
        }
        setAttr("roleList", Role.dao.getByUid(shopId));
        setAttr("brandStoreList", BrandStore.dao.getByShopId(shopId));
        setAttr("shopId",shopId);
        render(CommonUtil.TEMPLATE_PATH+"/business/usermanage/user_manage_input.html");
    }

    public void save(){
        String username=getPara("username");
        String password=getPara("password");
        String rePassword=getPara("rePassword");
        String email=getPara("email");
        String roleId=getPara("roleId");
        String shopId=getPara("shopId");
        String brandStoreId=getPara("brandStoreId");
        String userimgurl=getPara("userimgurl");
        String brandName=getPara("brandName");
        String category_one=getPara("category_one");
        String category_two=getPara("category_two");
        if(Member.dao.isExistByUsername(username,"0")){
            renderJson(Ret.fail("info","该用户名已被占用,请换个试试!"));
            return;
        }
        if (!StringUtils.equalsIgnoreCase(password, rePassword)) {
            renderJson(Ret.fail("info","两次密码输入不一致!"));
            return;
        }
        if(StrKit.isBlank(userimgurl)){
            renderJson(Ret.fail("info","头像不允许为空!"));
            return;
        }
        Member member=new Member();
        member.set("username", username);
        member.set("password", DigestUtils.md5Hex(password));
        member.set("brandName", brandName);
        member.set("categories",category_one+","+category_two);
        member.set("email", email);
        member.set("shopId","0");
        member.set("roleId", roleId);
        member.set("safeQuestion", null);
        member.set("safeAnswer", null);
        //member.set("memberRankId", MemberRank.dao.getDefaultMemberRank(shopId).getStr("id"));
        member.set("point", 0);
        member.set("deposit", new BigDecimal("0"));
        member.set("isAccountEnabled", true);
        member.set("isAccountLocked", false);
        member.set("loginFailureCount", 0);
        member.set("passwordRecoverKey", null);
        member.set("lockedDate", null);
        member.set("loginDate", new Date());
        member.set("registerIp",getRequest().getRemoteAddr());
        member.set("loginIp",getRequest().getRemoteAddr());
        member.set("id", CommonUtil.getUUID());
        member.set("createDate", new Date());
        member.set("b_shoprank",shopId);
        member.set("brandStoreId",brandStoreId);
        member.set("userimgurl",userimgurl);
        member.save();
        renderJson(Ret.ok("info","添加成功!").set("url","list"));

    }

    public void update(){
        String memberId=getPara("memberId");
        String password=getPara("password");
        String rePassword=getPara("rePassword");
        String email=getPara("email");
        String roleId=getPara("roleId");
        String brandStoreId=getPara("brandStoreId");
        String userimgurl=getPara("userimgurl");
        String brandName=getPara("brandName");
        String category_one=getPara("category_one");
        String category_two=getPara("category_two");
        if (!StringUtils.equalsIgnoreCase(password, rePassword)) {
            renderJson(Ret.fail("info","两次密码输入不一致!"));
            return;
        }
        if(StrKit.isBlank(userimgurl)){
            renderJson(Ret.fail("info","头像不允许为空!"));
            return;
        }
        Member member = Member.dao.findById(memberId);
        if(StrKit.notBlank(password)){
            member.set("password", DigestUtils.md5Hex(password));
        }
        member.set("email",email);
        member.set("roleId",roleId);
        member.set("brandStoreId",brandStoreId);
        member.set("userimgurl",userimgurl);
        member.set("brandName", brandName);
        member.set("categories",category_one+","+category_two);
        member.update();
        renderJson(Ret.ok("info","修改成功!").set("url","list"));

    }

}
